package com.muyu.danmo.service.impl;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.danmo.domain.DanmoTrainingInformation;
import com.muyu.danmo.domain.DanmoTrainingLikes;
import com.muyu.danmo.mapper.DanmoTrainingLikesMapper;
import com.muyu.danmo.service.IDanmoTrainingLikesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 培训班点赞Service业务层处理
 *
 * @author yangle
 * @date 2024-10-12
 */
@Service
public class DanmoTrainingLikesServiceImpl implements IDanmoTrainingLikesService
{
    @Autowired
    private DanmoTrainingLikesMapper danmoTrainingLikesMapper;

    /**
     * 查询培训班点赞
     *
     * @param id 培训班点赞主键
     * @return 培训班点赞
     */
    @Override
    public DanmoTrainingLikes selectDanmoTrainingLikesById(Long id)
    {
        return danmoTrainingLikesMapper.selectDanmoTrainingLikesById(id);
    }

    /**
     * 查询培训班点赞列表
     *
     * @param danmoTrainingLikes 培训班点赞
     * @return 培训班点赞
     */
    @Override
    public List<DanmoTrainingLikes> selectDanmoTrainingLikesList(DanmoTrainingLikes danmoTrainingLikes)
    {
        return danmoTrainingLikesMapper.selectDanmoTrainingLikesList(danmoTrainingLikes);
    }

    /**
     * 新增培训班点赞
     *
     * @param danmoTrainingLikes 培训班点赞
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDanmoTrainingLikes(DanmoTrainingLikes danmoTrainingLikes)
    {
        danmoTrainingLikes.setCreateTime(DateUtils.getNowDate());
        int rows = danmoTrainingLikesMapper.insertDanmoTrainingLikes(danmoTrainingLikes);
        insertDanmoTrainingInformation(danmoTrainingLikes);
        return rows;
    }

    /**
     * 修改培训班点赞
     *
     * @param danmoTrainingLikes 培训班点赞
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDanmoTrainingLikes(DanmoTrainingLikes danmoTrainingLikes)
    {
        danmoTrainingLikes.setUpdateTime(DateUtils.getNowDate());
        danmoTrainingLikesMapper.deleteDanmoTrainingInformationById(danmoTrainingLikes.getId());
        insertDanmoTrainingInformation(danmoTrainingLikes);
        return danmoTrainingLikesMapper.updateDanmoTrainingLikes(danmoTrainingLikes);
    }

    /**
     * 批量删除培训班点赞
     *
     * @param ids 需要删除的培训班点赞主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDanmoTrainingLikesByIds(Long[] ids)
    {
        danmoTrainingLikesMapper.deleteDanmoTrainingInformationByIds(ids);
        return danmoTrainingLikesMapper.deleteDanmoTrainingLikesByIds(ids);
    }

    /**
     * 删除培训班点赞信息
     *
     * @param id 培训班点赞主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDanmoTrainingLikesById(Long id)
    {
        danmoTrainingLikesMapper.deleteDanmoTrainingInformationById(id);
        return danmoTrainingLikesMapper.deleteDanmoTrainingLikesById(id);
    }

    @Override
    public Result likeTrainingClass(Long userId, Long trainingInformationId) {
        if (danmoTrainingLikesMapper.existsByUserIdAndTrainingClassId(userId, trainingInformationId)) {
            return Result.success( userId,"用户已点赞");
        }
        DanmoTrainingLikes danmoTrainingLikes = new DanmoTrainingLikes();
        danmoTrainingLikes.setUserId(userId);
        danmoTrainingLikes.setTrainingInformationId(trainingInformationId);
        insertDanmoTrainingLikes(danmoTrainingLikes);
        danmoTrainingLikesMapper.updateAmountPraise(trainingInformationId);
        return Result.success(userId,"点赞成功");
    }

    @Override
    public Long getLikesCount(Long trainingInformationId) {
        return danmoTrainingLikesMapper.countByTrainingClassId(trainingInformationId);

    }

    /**
     * 新增培训班详情信息
     *
     * @param danmoTrainingLikes 培训班点赞对象
     */
    public void insertDanmoTrainingInformation(DanmoTrainingLikes danmoTrainingLikes)
    {
        List<DanmoTrainingInformation> danmoTrainingInformationList = danmoTrainingLikes.getDanmoTrainingInformationList();
        Long id = danmoTrainingLikes.getId();
        if (StringUtils.isNotNull(danmoTrainingInformationList))
        {
            List<DanmoTrainingInformation> list = new ArrayList<DanmoTrainingInformation>();
            for (DanmoTrainingInformation danmoTrainingInformation : danmoTrainingInformationList)
            {
                danmoTrainingInformation.setId(id);
                list.add(danmoTrainingInformation);
            }
            if (list.size() > 0)
            {
                danmoTrainingLikesMapper.batchDanmoTrainingInformation(list);
            }
        }
    }
}
